package me.boot.auth.jap.service;

import cn.hutool.core.bean.BeanUtil;
import com.fujieid.jap.core.JapUser;
import me.boot.auth.jap.Util;
import me.boot.sys.entity.SysUserBinding;
import me.boot.sys.service.SysUserService;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    SysUserService sysUserService;

    @Override
    public JapUser getById(String userId) {
        return Util.fromSysUser(sysUserService.getById(userId));
    }

    @Override
    public JapUser getByUsername(String username) {
        return Util.fromSysUser(sysUserService.getByUsername(username));
    }

    @Override
    public boolean validPassword(String password, JapUser user) {
        return sysUserService.validPassword(Long.parseLong(user.getUserId()), password);
    }

    @Override
    public JapUser getByPlatformAndUid(String platformId, String uid) {
        return Util.fromSysUser(sysUserService.getByPlatformAndUid(platformId, uid));
    }

    @Override
    public JapUser createUser(AuthUser userInfo) {
        SysUserBinding userBinding = new SysUserBinding();
        BeanUtil.copyProperties(userInfo, userBinding, true);
        userBinding.setPlatformId(userInfo.getSource());
        return Util.fromSysUser(sysUserService.createUser(userBinding));
    }
}
